﻿using System;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace IFD2
{
    public partial class OpenFareDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            try
            {
                if (Session["ACL"].ToString().IndexOf("SA3") < 0)
                {
                    lblError.Text = "Unauthorized!";
                    return;
                }

                int dbf;
                SqlCommand cmd = new SqlCommand();
                SqlParameter para;
                DataTable dt_tmp = new DataTable();
                cmd.CommandText = "bll_fare_get_open_fare_details";
                dbf = Convert.ToInt16(Request.QueryString["DBF"]);
                para = cmd.Parameters.AddWithValue("@airline", Request.QueryString["AIRLINE"]);
                para = cmd.Parameters.AddWithValue("@fare_code", Request.QueryString["FARE_CODE"]);
                para = cmd.Parameters.AddWithValue("@ori_airport", Request.QueryString["ORI_AIRPORT"]);
                para = cmd.Parameters.AddWithValue("@des_airport", Request.QueryString["DES_AIRPORT"]);
                //para = cmd.Parameters.AddWithValue("@tax_type", Request.QueryString["TAX_TYPE"]);
                //para = cmd.Parameters.AddWithValue("@tax_valid", Request.QueryString["TAX_VALID"]);
                //para = cmd.Parameters.AddWithValue("@surcharge_valid", Request.QueryString["SURCHARGE_VALID"]);
                //para = cmd.Parameters.AddWithValue("@dbf", dbf);
                para = cmd.Parameters.AddWithValue("@lang", Request.QueryString["LANG"]);

                TinyDAL.Exec(ref cmd, ref dt_tmp);
                if (dt_tmp.Rows.Count == 1)
                {
                    lblFareName.Text = dt_tmp.Rows[0]["fare_name"].ToString();
                    lblFareCode.Text = dt_tmp.Rows[0]["code"].ToString();
                    lblLine.Text = dt_tmp.Rows[0]["trip_line"].ToString();
                    lblSeatMin.Text = dt_tmp.Rows[0]["seats_min"].ToString();
                    lblStayMin.Text = dt_tmp.Rows[0]["stay_min"].ToString();
                    lblStayMax.Text = dt_tmp.Rows[0]["stay_max"].ToString();
                    //lblChgNameFee.Text = dt_tmp.Rows[0]["chg_name_fee"].ToString();
                    //lblChgNameBefore.Text = dt_tmp.Rows[0]["chg_name_before"].ToString();
                    //lblChgFltFee.Text = dt_tmp.Rows[0]["chg_flt_fee"].ToString();
                    //lblChgFltBefore.Text = dt_tmp.Rows[0]["chg_flt_before"].ToString();

                    lblPrice.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["adt_price"]).ToString("0");
                    //lblAirportTax.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["adt_taxes"]).ToString("0");
                    //lblFuelCharge.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["adt_surcharge"]).ToString("0");
                    lblFreeBaggage.Text = dt_tmp.Rows[0]["free_baggage"].ToString();

                    lblChdPrice.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["chd_price"]).ToString("0");
                    //lblChdTax.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["chd_taxes"]).ToString("0");
                    //lblChdFuel.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["chd_surcharge"]).ToString("0");
                    lblChdBaggage.Text = dt_tmp.Rows[0]["chd_baggage"].ToString();

                    lblInfPrice.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["inf_price"]).ToString("0");
                    //lblInfTax.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["inf_taxes"]).ToString("0");
                    //lblInfFuel.Text = dt_tmp.Rows[0]["currency"] + Convert.ToDecimal(dt_tmp.Rows[0]["inf_surcharge"]).ToString("0");
                    lblInfBaggage.Text = dt_tmp.Rows[0]["inf_baggage"].ToString();

                    lblRules.Text = dt_tmp.Rows[0]["fare_rule"].ToString();
                }
                else
                {
                    lblRules.Text = "Duplicated Fare or Fare Not Found! Call fare manager.";
                    return;
                }


                //Open票只有DBF=0有效！阶梯收益票价显示取消！
                //TableRow tabRow;
                //TableCell tabCell;

                //cmd.Parameters.Clear();
                //DataTable dtStairs = new DataTable();
                //cmd.CommandText = "bll_fare_get_price_line";
                //para = cmd.Parameters.AddWithValue("@airline", Request.QueryString["AIRLINE"]);
                //para = cmd.Parameters.AddWithValue("@fare_code", Request.QueryString["FARE_CODE"]);
                //para = cmd.Parameters.AddWithValue("@seg_sn", Request.QueryString["SEG_SN"]);
                //para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                //para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                //para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                //TinyDAL.Exec(ref cmd, ref dtStairs);
                //int height;
                //tabRow = new TableRow();
                //foreach (DataRow rowAdtPrice in dtStairs.Rows)
                //{
                //    height = Convert.ToInt16(rowAdtPrice["adt_price"]) / 20;
                //    if (height < 20)
                //        height = 20;
                //    tabCell = new TableCell();
                //    if (dbf == Convert.ToInt16(rowAdtPrice["dbf"]))
                //        tabCell.Text = "<img src='images/bar_blue.bmp' width=10 height=" + height.ToString() + " title=" + Convert.ToSingle(rowAdtPrice["adt_price"]).ToString() + "><br/>" + rowAdtPrice["dbf"].ToString();
                //    else
                //        tabCell.Text = "<img src='images/bar_green.bmp' width=10 height=" + height.ToString() + " title=" + Convert.ToSingle(rowAdtPrice["adt_price"]).ToString() + "><br/>" + rowAdtPrice["dbf"].ToString();
                //    tabCell.VerticalAlign = VerticalAlign.Bottom;
                //    tabCell.HorizontalAlign = HorizontalAlign.Center;
                //    tabRow.Cells.Add(tabCell);

                //}
                //tabAdtPrice.Rows.Add(tabRow);

                //tabRow = new TableRow();
                //foreach (DataRow rowChdPrice in dtStairs.Rows)
                //{
                //    height = Convert.ToInt16(rowChdPrice["chd_price"]) / 20;
                //    if (height < 20)
                //        height = 20;
                //    tabCell = new TableCell();
                //    if (dbf == Convert.ToInt16(rowChdPrice["dbf"]))
                //        tabCell.Text = "<img src='images/bar_blue.bmp' width=10 height=" + height.ToString() + " title=" + Convert.ToSingle(rowChdPrice["chd_price"]).ToString() + "><br/>" + rowChdPrice["dbf"].ToString();
                //    else
                //        tabCell.Text = "<img src='images/bar_green.bmp' width=10 height=" + height.ToString() + " title=" + Convert.ToSingle(rowChdPrice["chd_price"]).ToString() + "><br/>" + rowChdPrice["dbf"].ToString();
                //    tabCell.VerticalAlign = VerticalAlign.Bottom;
                //    tabCell.HorizontalAlign = HorizontalAlign.Center;
                //    tabRow.Cells.Add(tabCell);

                //}
                //tabChdPrice.Rows.Add(tabRow);

                //tabRow = new TableRow();
                //foreach (DataRow rowInfPrice in dtStairs.Rows)
                //{
                //    height = Convert.ToInt16(rowInfPrice["inf_price"]) / 20;
                //    if (height < 20)
                //        height = 20;
                //    tabCell = new TableCell();
                //    if (dbf == Convert.ToInt16(rowInfPrice["dbf"]))
                //        tabCell.Text = "<img src='images/bar_blue.bmp' width=10 height=" + height.ToString() + " title=" + Convert.ToSingle(rowInfPrice["inf_price"]).ToString() + "><br/>" + rowInfPrice["dbf"].ToString();
                //    else
                //        tabCell.Text = "<img src='images/bar_green.bmp' width=10 height=" + height.ToString() + " title=" + Convert.ToSingle(rowInfPrice["inf_price"]).ToString() + "><br/>" + rowInfPrice["dbf"].ToString();
                //    tabCell.VerticalAlign = VerticalAlign.Bottom;
                //    tabCell.HorizontalAlign = HorizontalAlign.Center;
                //    tabRow.Cells.Add(tabCell);

                //}
                //tabInfPrice.Rows.Add(tabRow);

                dt_tmp.Clear();
                StringBuilder tmp = new StringBuilder();
                cmd.Parameters.Clear();
                cmd.CommandText = "bll_query_get_open_flt_surcharge_details";
                para = cmd.Parameters.AddWithValue("@airline", Request.QueryString["AIRLINE"]);
                para = cmd.Parameters.AddWithValue("@ori_airport", Request.QueryString["ORI_AIRPORT"]);
                para = cmd.Parameters.AddWithValue("@des_airport", Request.QueryString["DES_AIRPORT"]);
                para = cmd.Parameters.AddWithValue("@fare_currency", lblPrice.Text);
                para = cmd.Parameters.AddWithValue("@lang", Session["LANG"].ToString());
                TinyDAL.Exec(ref cmd, ref dt_tmp);
                if (dt_tmp.Rows.Count == 0)
                {
                    lblError.Text = "Taxes & surcharges not found!";
                    return;
                }
                tmp.Append("<tr><td colspan='7'>Taxes and Surcharges</td></tr><tr><td>Surcharge Code</td><td>Name</td><td>Adt. Price</td><td>Chd. Price</td><td>Inf. Price</td></tr>");
                foreach (DataRow row in dt_tmp.Rows)
                {
                    tmp.Append("<tr><td>&bull; " + row["code"].ToString().TrimEnd() + "(" + row["type"].ToString() + ")</td><td>" + row["name"].ToString().TrimEnd() + "</td><td>"
                        + row["currency"].ToString() + row["adt_price"].ToString() + "(≈" + row["fare_currency"].ToString() + (Convert.ToDecimal(row["adt_price"]) * Convert.ToDecimal(row["exch_rate"])).ToString() + ")</td><td>"
                        + row["currency"].ToString() + row["chd_price"].ToString() + "(≈" + row["fare_currency"].ToString() + (Convert.ToDecimal(row["chd_price"]) * Convert.ToDecimal(row["exch_rate"])).ToString() + ")</td><td>"
                        + row["currency"].ToString() + row["inf_price"].ToString() + "(≈" + row["fare_currency"].ToString() + (Convert.ToDecimal(row["inf_price"]) * Convert.ToDecimal(row["exch_rate"])).ToString() + ")</td></tr>");
                }
                litSurcharges.Text = tmp.ToString();

            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text=ex.Message;
                else
                    lblError.Text="Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";

                return;
            }
        }
    }
}